#include "paramChooseDialog.h"

paramChooseDialog::paramChooseDialog( QWidget *parent ) : QDialog( parent )
{
    state = 0;

    inputParam = new inputParamWidget( this );
    param = new paramWidget(this);
    sheet = new spredsheet( this );

    stepList = new QListWidget();
    stepList->addItems( steps );
    stepList->setEnabled( false );
    stepList->setMaximumWidth( 200 );

    captionLable = new QLabel();

    backButton = new QPushButton( tr("<<Back") );
    nextButton = new QPushButton( tr("Next>>") );
    closeButton = new QPushButton( tr("Close") );

    updateDialog();

    createMainLayout();

    setWindowTitle( tr("Creating new task") );

    connect( nextButton, SIGNAL(clicked()), this, SLOT(nextParamDlg()) );
    connect( backButton, SIGNAL(clicked()), this, SLOT(prevParamDlg()) );
    connect( inputParam->inputBrowsePush, SIGNAL(clicked()), this, SLOT(browseInputFile()) );
    connect( closeButton, SIGNAL(clicked()), this, SLOT(close()) );
    connect( inputParam->editManualPush, SIGNAL(clicked()),
             this, SLOT( manualEditInput()) );
    connect( sheet, SIGNAL(tableMade(QVector<QVector<int> >)),
             this, SLOT(tableMade(QVector<QVector<int> >)));
}

void paramChooseDialog::updateDialog()
{
    captionLable->setText( "<FONT SIZE=10>" + steps[state] + "</FONT>" );

    stepList->setCurrentRow( state );

    if (state == 0){
        nextButton->setText( tr("Next>>") );
        backButton->setVisible( false );
        param->setVisible( false );
        inputParam->setVisible( true );
    } else
        if (state == 1){
            nextButton->setText( tr("Finish") );
            backButton->setVisible( true );
            inputParam->setVisible( false );
            param->setVisible( true );
        }
}

void paramChooseDialog::createMainLayout()
{
    paramLayout = new QVBoxLayout();
    paramLayout->addWidget( captionLable, 0, Qt::AlignCenter );
    paramLayout->addWidget( inputParam, 0, Qt::AlignLeft );
    paramLayout->addWidget( param, 0, Qt::AlignLeft );

    infoLayout = new QHBoxLayout();
    infoLayout->addWidget( stepList );
    infoLayout->addLayout( paramLayout );

    buttonsLayout = new QHBoxLayout();
    buttonsLayout->addStretch();
    buttonsLayout->addWidget( backButton );
    buttonsLayout->addWidget( nextButton );
    buttonsLayout->addWidget( closeButton );


    mainLayout = new QVBoxLayout();
    mainLayout->addLayout( infoLayout );
    mainLayout->addLayout( buttonsLayout );

    setLayout( mainLayout );
}

void paramChooseDialog::nextParamDlg()
{
    QString str;
    if (state == 0){
        //checking for not empty task name
        str.clear();
        str = inputParam->taskNameEdit->text();
        if ( str.isEmpty() ){
            QMessageBox::warning( this, tr("warning"), tr("Name is empty") );
            return;
        } else task.setName( str );             //NAME is set

        //checking for input choosen
        if (inputParam->fromFileRadio->isChecked())
        {
            //check for input file is choosen
            if (inputFileName.isEmpty()){
                QMessageBox::warning( this, tr("warning"), tr("Input file is not choosen") );
                return;
            } else {
                //loading input table
                int n, m;
                QFile file(inputFileName);
                file.open( QFile::ReadOnly );

                QTextStream in(&file);
                in >> n >> m;

                task.input.resize( n );
                for (int i=0; i<n; i++)
                    task.input[i].resize( m );

                for (int i=0; i<n; i++)
                    for (int j=0; j<m; j++)
                        in >> task.input[i][j];         //INPUT TABLE is set
            }
        } else {
            //means manual input is choosen
            if (!manualInputChoosen){
                QMessageBox::warning( this, tr("warning"), tr("Input is not maked manualy") );
                return;
            } else {
                task.input = inputTable;                //INPUT TABLE is set
            }

        }

    } else {
        //means state == 1
        task.setAlgorithm( param->algoBox->currentIndex() );              //ALGORITHM is set
        task.setOptimization( param->optBox->currentIndex() );           //OPTIMIZATION is set
        task.setDate( QDate::currentDate() );                                  //DATE is set

        emit itemMade( task );
        close();
    }

    if (state != dlgNumber-1){
        state++;
        updateDialog();
    }
}

void paramChooseDialog::prevParamDlg()
{
    if (state != 0){
        state--;
        updateDialog();
    }
}

void paramChooseDialog::browseInputFile()
{
    inputFileName.clear();
    inputFileName = QFileDialog::getOpenFileName( this, tr("Open input file"), 0 );

    inputParam->inputFileEdit->setText( inputFileName );
}

void paramChooseDialog::setReason( int reason, taskType item )
{
    clearDialog();
    if (reason == 1) setTask( item );
    updateDialog();
    //0 - new
    //1 - edit
}

void paramChooseDialog::setTask(taskType task)
{
    this->task = task;

    state = 0;
    fileInputChoosen = true;
    manualInputChoosen = false;

    inputParam->taskNameEdit->setText( task.getName() );
    inputParam->inputFileEdit->setText( inputPrefix + task.getName() );
    inputParam->fromFileRadio->setChecked( true );

    inputFileName = inputParam->inputFileEdit->text();

    param->algoBox->setCurrentIndex( algo.indexOf( task.getAlgorithm() ) );
    //int k = optim.indexOf( task.getOptimization());
    param->optBox->setCurrentIndex( /*optim.indexOf( task.getOptimization() )*/0 );

}

void paramChooseDialog::clearDialog()
{
    state = 0;

    fileInputChoosen = false;
    manualInputChoosen = false;

    inputParam->taskNameEdit->clear();
    inputParam->inputFileEdit->clear();
    inputParam->machinesEdit->clear();
    inputParam->detailsEdit->clear();
    inputParam->fromFileRadio->setChecked( true );

    param->algoBox->setCurrentIndex( 0 );
    param->optBox->setCurrentIndex( 0 );

    sheet->clear();
}

void paramChooseDialog::
        tableMade( QVector<QVector<int> > inputTable )
{
    manualInputChoosen = true;
    this->inputTable = inputTable;
}

void paramChooseDialog::manualEditInput()
{
    int n, m;
    bool ok_n, ok_m;

    m = inputParam->detailsEdit->text().toInt( &ok_n );
    n = inputParam->machinesEdit->text().toInt( &ok_m );
    if (!ok_n || !ok_m){
        QMessageBox::warning( this, tr("warning"),
                              tr("Number of details or machines is not valid number") );
        return;
    }

    sheet->setSize( n, m );
    sheet->exec();
}


inputParamWidget::inputParamWidget( QWidget *parent ) : QWidget (parent)
{
    fromFileRadio = new QRadioButton(tr("From file:"));
    manualRadio = new QRadioButton(tr("Manual:"));

    inputBrowsePush = new QPushButton(tr("Browse"));
    editManualPush = new QPushButton(tr("Edit"));
    editManualPush->setEnabled( false );

    inputFileEdit = new QLineEdit();
    inputFileEdit->setEnabled( false );
    detailsEdit = new QLineEdit();
    detailsEdit->setEnabled( false );
    machinesEdit = new QLineEdit();
    machinesEdit->setEnabled( false );
    taskNameEdit = new QLineEdit();

    detailsLabel = new QLabel(tr("Details:"));
    machinesLabel = new QLabel(tr("Machines:"));
    taskNameLabel = new QLabel(tr("Task name:"));

    inputFileLayout = new QHBoxLayout();
    inputFileLayout->addWidget( inputFileEdit );
    inputFileLayout->addWidget( inputBrowsePush );

    nmParamLayout = new QHBoxLayout();
    nmParamLayout->addWidget( detailsLabel);
    nmParamLayout->addWidget( detailsEdit );
    nmParamLayout->addWidget( machinesLabel );
    nmParamLayout->addWidget( machinesEdit );
    nmParamLayout->addWidget( editManualPush );

    mainLayout = new QVBoxLayout();
    mainLayout->addWidget(taskNameLabel );
    mainLayout->addWidget( taskNameEdit );
    mainLayout->addWidget( fromFileRadio );
    mainLayout->addLayout( inputFileLayout );
    mainLayout->addWidget( manualRadio );
    mainLayout->addLayout( nmParamLayout );
    mainLayout->addStretch();

    setLayout( mainLayout );

    connect( fromFileRadio, SIGNAL(toggled(bool)), inputBrowsePush, SLOT(setEnabled(bool)) );
    connect( manualRadio, SIGNAL(toggled(bool)), detailsEdit, SLOT(setEnabled(bool)) );
    connect( manualRadio, SIGNAL(toggled(bool)), machinesEdit, SLOT(setEnabled(bool)) );
    connect( manualRadio, SIGNAL(toggled(bool)), editManualPush, SLOT(setEnabled(bool)) );

    fromFileRadio->click();
}

paramWidget::paramWidget( QWidget *parent ) : QWidget( parent )
{
    algoLabel = new QLabel(tr("Algorithm type"));
    optLabel = new QLabel(tr("Optimization type"));

    algoBox = new QComboBox();
    algoBox->addItems( algo );

    optBox = new QComboBox();
    optBox->addItems( optim );

    mainLayout = new QVBoxLayout();
    mainLayout->addWidget( algoLabel );
    mainLayout->addWidget( algoBox );
    mainLayout->addWidget( optLabel );
    mainLayout->addWidget( optBox );

    setLayout( mainLayout );
}
